package com.example.android.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.lifecycle.lifecycleScope
import com.example.android.R
import com.example.android.extensions.AlertDialog
import com.example.android.extensions.Services
import com.example.android.models.DormInfo
import com.example.android.models.Dorm_1
import com.example.android.utils.models.MyResult
import kotlinx.android.synthetic.main.fragment_select_bed2.*
import kotlinx.android.synthetic.main.fragment_select_bed2.pre
import kotlinx.android.synthetic.main.fragment_select_bed3.*
import kotlinx.android.synthetic.main.fragment_select_bed3.view.*
import kotlinx.android.synthetic.main.fragment_select_bed_finish.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [SelectBed3Fragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class SelectBed3Fragment(get: String, get1: String) : Fragment() {

    val region = get
    val dormNumber = get1
    // TODO: Rename and change types of parameters
    var bedId : Int? = null
    var flag = 0
    var dormId : Int? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_select_bed3, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

//        val stu_name_text = findViewById<TextView>(R.id.stu_name)
//
//        val builder = AlertDialog.Builder(stu_name_text.context)
//            //val builder: AlertDialog.Builder = AlertDialog.Builder(this)
//        builder.setTitle("尊敬的用户")
//        builder.setMessage("你真的要卸载我吗？")

        //val next_button = view.findViewById<Button>(R.id.next)

        lifecycleScope.launch(Dispatchers.Main) {



            val bedList : ArrayList<Dorm_1>? = Services.SelectBedService.GetBed(region,dormNumber).Data
            Log.d(toString(),bedList.toString())
            dormId = bedList?.get(0)?.dormId?.toInt()?.div(6)
            Log.d(toString(),dormId.toString())

            val button1 = view.findViewById<Button>(R.id.bed1)
            val button2 = view.findViewById<Button>(R.id.bed2)
            val button3 = view.findViewById<Button>(R.id.bed3)
            val button4 = view.findViewById<Button>(R.id.bed4)
            val button5 = view.findViewById<Button>(R.id.bed5)
            val button6 = view.findViewById<Button>(R.id.bed6)

            bedList?.forEach{
                Log.d(toString(),it.dormId + " " + it.choose)
                //dormId = it.dormId.toInt() / 6

                if(it.choose == 1){
                    val bedId = it.dormId.toInt() % 6
                    if (bedId == 1){
                        button1.isEnabled = false
                    }
                    if (bedId == 2){
                        button2.isEnabled = false
                    }
                    if (bedId == 3){
                        button3.isEnabled = false
                    }
                    if (bedId == 4){
                        button4.isEnabled = false
                    }
                    if (bedId == 5){
                        button5.isEnabled = false
                    }
                    if (bedId == 6){
                        button6.isEnabled = false
                    }
                }


            }

        }




        pre.setOnClickListener {
            val select_bed_step2 = SelectBed2Fragment()

            requireActivity().supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(
                    R.anim.fragment_item_select_in,
                    R.anim.fragment_item_select_out,
                    R.anim.fragment_item_back_in,
                    R.anim.fragment_item_back_out,
                )
                .replace(R.id.content_main_fragment,select_bed_step2)
                .addToBackStack(null)
                .commit()
        }
        check.setOnClickListener {
            if(flag == 0){
                AlertDialog {
                    it.SetContent("请选择床位！")
                    it.SetOnSureButton {
                        it.dismiss()
                    }
                    it.SetOnCancelButton {
                        it.dismiss()
                    }
                }
            }
            if(flag == 1){

                lifecycleScope.launch(Dispatchers.Main) {
                    Log.d(toString(),"!!!!!!!"+ dormId.toString())

                    val data = Services.SelectBedService.PostDormId(dormId.toString())
                    Log.d(toString(),data.Code.toString())
                    if(data.Code == MyResult.OK){
                        val select_bed_step4 = SelectBedFinishFragment()
//                        val dorm = DormInfo(
//
//                        )
                        // added by kamanri, 2022/6/7
                        // pop all backstack
                        requireActivity().supportFragmentManager.apply {
                            for( i in 0..3) {
                                popBackStack()
                            }
                        }

                        requireActivity().supportFragmentManager
                            .beginTransaction()
                            .setCustomAnimations(
                                R.anim.fragment_item_select_in,
                                R.anim.fragment_item_select_out,
                                R.anim.fragment_item_back_in,
                                R.anim.fragment_item_back_out,
                            )
                            .replace(R.id.content_main_fragment,select_bed_step4)
                            .commit()

                    }
                }


            }

        }
        bed1.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择①号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:1"
                    flag = 1
                    bedId = 1
                    dormId = dormId?.times(6)?.plus(bedId!!)

                    Log.d(toString(),dormId.toString())

                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }
        bed2.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择②号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:2"
                    flag = 1
                    bedId = 2
                    dormId = dormId?.times(6)?.plus(bedId!!)
                    Log.d(toString(),"dormID ===="+ dormId.toString())
                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }
        bed3.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择③号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:3"
                    flag = 1
                    bedId = 3
                    dormId = dormId?.times(6)?.plus(bedId!!)

                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }
        bed4.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择④号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:4"
                    flag = 1
                    bedId = 4
                    dormId = dormId?.times(6)?.plus(bedId!!)

                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }
        bed5.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择⑤号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:5"
                    flag = 1
                    bedId = 5
                    dormId = dormId?.times(6)?.plus(bedId!!)

                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }
        bed6.setOnClickListener {
            AlertDialog {
                it.SetContent("确认选择⑥号床")
                it.SetOnSureButton {
                    it.dismiss()
                    text_read.text = "你已选择床位:6"
                    flag = 1
                    bedId = 6
                    dormId = dormId?.times(6)?.plus(bedId!!)

                }
                it.SetOnCancelButton {
                    it.dismiss()
                }
            }
        }





    }



}